// JavaScript Document
var d = function(){
	function myDate(){
		var obj	;
		var frame ;
		var year ;
		var month;
		var day ;
		var week;
		this.addDateTo = function(o){
			obj = o;
			init();
			show();
		}
		var init = function(){
			var d = new Date();
			year = d.getFullYear();
			month = d.getMonth()+1;
			day = d.getDate();
			week = d.getDay();
			if(week ==0){
				week = 7;	
			}
		}
		//展示方法
		var show = function(){
			frame = document.createElement("secton");	
			frame.style.cssText = "width:100%;height:100%;backgroundColor:red;";
			addHead();
			addBody();
			obj.appendChild(frame);
		}
		//添加头信息
		var addHead = function(){
			var header = document.createElement("header");
			header.style.cssText = "font-size:14px;width:100%;height:20px;";
			header.innerHTML = "<h4 style = 'color:#ffac50;'>"+year+"年"+month+"月"+"</h4>";
			frame.appendChild(header);
		}
		var addBody = function(){
			var t = document.createElement("table");
			t.style.cssText = "border:1px solid #ffac50;width:98%;height:90%;margin:3px;border-collapse:collapse;";
			t.cellspacing="0";	
			t.border = "0";
			var tbody = document.createElement("TBODY");
			var tr = document.createElement("TR");
			tr.style.cssText = "background-Color:#f1b253;border:none;"
			for(var i =0;i<7;i++){
				var td = document.createElement("TD");			
				if(i ==0){
					td.innerHTML = "日";				
				}
				else if(i ==1){
					td.innerHTML = "一";
				}
				else if(i ==2){
					td.innerHTML = "二";
				}
				else if(i ==3){
					td.innerHTML = "三";
				}
				else if(i ==4){
					td.innerHTML = "四";
				}
				else if(i ==5){
					td.innerHTML = "五";
				}
				else if(i ==6){
					td.innerHTML = "六";
				}
				tr.appendChild(td);				
			}
			tbody.appendChild(tr);
			var tr_1 = document.createElement("TR");	
			for(var i =0;i<getIndex();i++){
				tr_1.style.cssText = "border:1px solid #fff;";
				var td = document.createElement("TD");
				td.style.cssText = "width:31px;height:34px;background-Color:#fcecd3;border:1px solid #fff;";			
				td.innerHTML = "";
				tr_1.appendChild(td);
			}
			var size =0;
			for(var i = getIndex();i<7;i++){
				tr_1.style.cssText = "border:1px solid #fff;";
				var td = document.createElement("TD");
				td.style.cssText = "cursor:pointer;width:31px;height:34px;background-Color:#fcecd3;border:1px solid #fff;";			
				td.innerHTML = ++size;
				if(size == day){
					td.style.cssText = td.style.cssText + "background-Color:#ffac50;";	
				}
				tr_1.appendChild(td);		
			}
			
			tbody.appendChild(tr_1);
			for(var i =0;i<5;i++){				
				var tr = document.createElement("TR");	
				tr.style.cssText = "border:1px solid #fff;";		
				for(var j =0;j<7;j++){
					var td = document.createElement("TD");
					td.style.cssText = "cursor:pointer;background-Color:#fcecd3;border:1px solid #fff;";			
					td.innerHTML = ++size;
					if(size == day){
						td.style.cssText = td.style.cssText + "background-Color:#ffac50;";	
					}
					tr.appendChild(td);
					if(size == getMaxDay()){
						while(++j<7){
							var td = document.createElement("TD");
							td.style.cssText = "width:31px;height:34px;background-Color:#fcecd3;border:1px solid #fff;";			
							td.innerHTML = "";
							tr.appendChild(td);
							
						}
						break;	
					}
				}			
				tbody.appendChild(tr)
				if(size == getMaxDay()){
					var tr = document.createElement("TR");	
					for(var m =i+1;m<5;m++){
						for(var n = 0;n<7;n++){
							var td = document.createElement("TD");
							td.style.cssText = "width:31px;height:34px;background-Color:#fcecd3;border:1px solid #fff;";			
							td.innerHTML = "";
							tr.appendChild(td);
						}	
					}	
					tbody.appendChild(tr)
					break;
				}
				else{
					tbody.appendChild(tr)
				}
			}		
			t.appendChild(tbody);
			frame.appendChild(t);	
		}
		var getIndex = function(){
			var index = day%7;
			if(index<=week){
				return week -index + 1;
			}
			else{
				return 7 + week -index + 1;
			}
		}
		var getMaxDay = function(){
	        var curDate = new Date();
			/* 获取当前月份 */
			var curMonth = curDate.getMonth();
		    /*  生成实际的月份: 由于curMonth会比实际月份小1, 故需加1 */
		    curDate.setMonth(curMonth + 1);
		    /* 将日期设置为0, 这里为什么要这样设置, 我不知道原因, 这是从网上学来的 */
		    curDate.setDate(0);
		    /* 返回当月的天数 */
		    return curDate.getDate();
		}
	}	
	return new myDate();
}